﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MangoMes.Write.API.Applications.Pickinges;
using MediatR;
using System.Collections.Generic;

namespace MangoMes.Write.API.Applications.ArrivalNotice
{
    public class ArrivalNoticesSeachHandler : IRequestHandler<ArrivalNoticesSeachCommand, PageDTO<ArrivalNoticeDto>>
    {
        #region 仓储 
        /// <summary>
        /// 生产领料仓储
        /// </summary>
        private readonly IBateRepository<ArrivalNoticeDto> ArrivalNoticeDtoRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ArrivalNoticeDtoRepository"></param>
        public ArrivalNoticesSeachHandler(IBateRepository<ArrivalNoticeDto> ArrivalNoticeDtoRepository)
        {
            this.ArrivalNoticeDtoRepository = ArrivalNoticeDtoRepository;
        }
        #endregion

        #region 到货通知单 列表
        /// <summary>
        /// 到货通知单 列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageDTO<ArrivalNoticeDto>> Handle(ArrivalNoticesSeachCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //PageDTO<ArrivalNoticeDto>对象result，并设置Code为 0
                PageDTO<ArrivalNoticeDto> result = new PageDTO<ArrivalNoticeDto>();

                var pageNum = (request.pageIndex - 1) * request.pageSize;
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper dapperHelper = new DapperHelper();
                ArrivalNoticeDto dto = new ArrivalNoticeDto();

                //连接mySql 
                string conn = dapperHelper.MangoMesWrite;

                //sql语句
                string str = $"select * from arrivalnotice a join purchasestorage b on a.ArrivalNoticeId=b.ArrivalNoticeId join supplier c on a.SupplierName=c.SupplierName";
                //string str = $"select * from arrivalnotice a join supplier b on a.SupplierName=b.SupplierName join purchasestorage c on a.ArrivalNoticeId=c.ArrivalNoticeId";

                var list = ArrivalNoticeDtoRepository.GetAll(str, dto, conn).ToList();

                //通知单编号
                if (!string.IsNullOrEmpty(request.ArrivalNoticeNode))
                {
                    list = list.Where(x => x.ArrivalNoticeNode.Contains(request.ArrivalNoticeNode)).ToList();
                }
                //通知单名称
                if (!string.IsNullOrEmpty(request.ArrivalNoticeName))
                {
                    list = list.Where(x => x.ArrivalNoticeName.Contains(request.ArrivalNoticeName)).ToList();
                }
                //采购订单编号
                if (!string.IsNullOrEmpty(request.PurchaseIndentId))
                {
                    list = list.Where(x => x.PurchaseIndentId.Contains(request.PurchaseIndentId)).ToList();
                }
                //供应商名称
                if (!string.IsNullOrEmpty(request.SupplierName))
                {
                    list = list.Where(x => x.SupplierName.Contains(request.SupplierName)).ToList();
                }
                //到货日期
                if (request.ArrivalNoticeTime != null)
                {
                    list = list.Where(x => x.ArrivalNoticeTime == request.ArrivalNoticeTime).ToList();
                }
                //单据状态
                if (request.Documents > 0)
                {
                    list = list.Where(x => x.Documents == request.Documents).ToList();
                }

                result.TotalCount = list.Count(); //计算list列表中元素个数
                result.PageCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pageSize); //表示总的记录数量
                list = list.OrderByDescending(x => x.ArrivalNoticeId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();//计算总页数
                result.Data = list;

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
